import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class Emcee implements Iterable<Instructor>, EmceeInterface {
	private List<Instructor> victims;
	private ThrowerInterface currentThrower;
	private InstructorInterface currentVictim;

	public Emcee() {
		victims = new ArrayList<Instructor>();
		currentThrower = new Thrower(0);
		currentVictim = new Instructor("");
	}

	@Override
	public ThrowerInterface getCurrentThrower() {
		return currentThrower;
	}

	@Override
	public InstructorInterface getCurrentVictim() {
		return currentVictim;
	}

	@Override
	public void throwerStepsUp(ThrowerInterface nextThrower) {
		currentThrower = nextThrower;
	}

	@Override
	public void throwerChoosesVictim(Instructor victim) {
		int position = victims.indexOf(victim);
		if (position == -1) {
			victims.add(victim);
			currentVictim = victim;
			return;
		}

		currentVictim = victims.get(position);
	}

	@Override
	public boolean throwAnother(int fundsPayed) {
		if (currentThrower.hasNext() == false) {
			return false;
		}

		currentThrower.next();

		currentVictim.addToMoneyRaised(fundsPayed);
		return true;
	}

	@Override
	public void dunked() {
		currentVictim.wasDunked();
	}

	@Override
	public Iterator<Instructor> iterator() {
		Collections.sort(victims);
		Collections.reverse(victims);
		return victims.iterator();
	}

	@Override
	public int getTotalFundsRaised() {
		int total = 0;

		for (Instructor who : victims) {
			total += who.getMoneyRaised();
		}

		return total;
	}
}
